package com.qf.ts.platform.front.web;

import com.github.pagehelper.PageInfo;
import com.qf.ts.platform.commons.pojo.R;
import com.qf.ts.platform.commons.pojo.TableData;
import com.qf.ts.platform.front.entity.CarSource;
import com.qf.ts.platform.front.service.CarSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xianchang
 */
@RestController
@RequestMapping("/carSource")
@Api(value = "车源信息的增删改查")
public class CarSourceController {

    @Resource
    private CarSourceService carSourceService;

    @GetMapping
    @ApiOperation(value = "查询车源列表", notes = "本接口用来查询符合条件的车源信息")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "car", value = "车源查询条件", required = true, dataTypeClass = Car.class),
            @ApiImplicitParam(name = "page", value = "查询后显示起始页数", required = true, dataType = "Integer", example = "1"),
            @ApiImplicitParam(name = "limit", value = "查询后每页显示条数", required = true, dataType = "Integer", example = "10")
    })
    public TableData getCarSource(CarSource carSource, Integer page, Integer limit) {
        PageInfo<CarSource> pageInfo = carSourceService.getCarSource(carSource, page, limit);
        return new TableData<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping
    @ApiOperation(value = "新增车源信息")
//    @ApiImplicitParam(name = "car", value = "车源发布的车辆信息", required = true, dataTypeClass = Car.class)
    public R addCarSource(CarSource carSource) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String releaseTime = format.format(new Date());
        carSource.setReleaseTime(releaseTime);
        try {
            carSourceService.addCarSource(carSource);
            return new R(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R(false, "添加失败");
    }

    @PutMapping
    @ApiOperation(value = "更新车源信息")
    public R updateCarSource(CarSource carSource) {
        try {
            carSourceService.updateCarSource(carSource);
            return new R(true,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R(false, "更新失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除车源信息")
    public R deleteCarSource(Integer id) {
        try {
            carSourceService.deleteCarSource(id);
            return new R(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R(false,"删除失败");
    }

}
